
#include <string>
#include <fstream>
#include <stdlib.h>
#include <iostream>
#include <vector>

using namespace std;

class Grapher{


};

const string RewardEpisode("RewardEpisode:");
const string TotalEpisodes("TotalEpisodes");
const string NumRewards("NumRewards:");

int main(int argc, char **argv){

	std::cout<<"usage:: input output averageOver"<<std::endl;

	string input = argv[1];
	string output = argv[2];
    int averageOver = 1;
    averageOver = atoi(argv[3]);

	std::ofstream outReward;
	outReward.open(output.c_str());

	  std::ifstream reward;
	  reward.open(input.c_str());

	  string s;

	  reward>>s;
	  int numEpisodes;
	  if(s.compare(TotalEpisodes)==0){
		  reward>>numEpisodes;
	  }

	  numEpisodes--;

	  std::vector<double> rewards;

	  for(int i=0; i< numEpisodes; i++){
		  int numSteps;
		  int episodeNum;
		  reward>>s;
		  if(s.compare(RewardEpisode) == 0){
			  reward>>episodeNum;
		  }
		  reward>>s;
		  if(s.compare(NumRewards) == 0){
			  reward>>numSteps;
		  }

		  double totalReward = 0.0;
		  for(int j=0; j< numSteps; j++){
			  double x;
			  reward>>x;
			  totalReward += x;
		  }
		  rewards.push_back(totalReward);
	  }


	  for(int i=0; i< numEpisodes-averageOver+1; i++){
		  double rewardSum = 0.0;
		  for(int j=0; j< averageOver; j++){
			  rewardSum += rewards[i+j];
		  }
		  rewardSum /= averageOver;

		  outReward<<i<<"\t"<<rewardSum<<std::endl;
	  }



	  reward.close();
	  outReward.close();

}
